package com.zyn.qianwen.user.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;


public class JwtUtil {

	/**
	 * 生成JWT
	 *
	 * @param id
	 * @param subject
	 * @return
	 */
	public String createJWT(String id, String subject, String roles) {
		long nowMillis = System.currentTimeMillis();
		Date now = new Date(nowMillis);
		JwtBuilder builder = Jwts.builder().setId(id).setSubject(subject).setIssuedAt(now)
				.signWith(SignatureAlgorithm.HS256, "zyn2091").claim("roles", roles);
		builder.setExpiration(new Date(nowMillis + 18000000));
		return builder.compact();
	}

	/**
	 * 解析JWT
	 *
	 * @param jwtStr
	 * @return
	 */
	public Claims parseJWT(String jwtStr) {
		return Jwts.parser().setSigningKey("zyn2091").parseClaimsJws(jwtStr).getBody();
	}
}
